<?php
/**
 http://www.i7wx.com/modules/article/search.php
 http://www.263zw.com/modules/article/index.php?fullflag=1
 https://www.ybdu.com/xiaoshuo/3/3553/
 */
class parseHtml {
    function getBook($url, $html) {
        $parseurl = parse_url($url);
        if($parseurl['host'] == 'www.woquge.com') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $last_update_time = cut('<p>最后更新：', '</p>', $result);
            $last_update_time = strtotime($last_update_time);
            $description = cut('<div id="intro">', '</div>', $result);

            $body = cut('正文</dt>', '</dl>', $result);
            preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $body, $matches);
        }
        elseif ($parseurl['host'] == 'www.biquge.com.tw') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $last_update_time = cut('<p>最后更新：', '</p>', $result);
            $last_update_time = strtotime($last_update_time);
            $description = cut('<div id="intro">', '</div>', $result);

            $body = cut('<div id="list">', '</div>', $result);
            preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $body, $matches);
        }
        elseif ($parseurl['host'] == 'www.vodtw.com') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $last_update_time = cut('<li>更新时间：', '</li>', $result);
            $last_update_time = strtotime($last_update_time);
            $description = cut('<div class="bookintro">', '</div>', $result);

            $body = cut('<div class="insert_list">', '</div>', $result);
            preg_match_all('/<a href="(.*?)"[^>]*>(.*?)<\/a>/i', $body, $matches);
        }
        else {
            print_r($parseurl);
            return false;
        }

        return [$last_update_time, $description, $matches, $parseurl];
    }

    function getContent($url, $html, $snoopy = null) {
        $parseurl = parse_url($url);
        if($parseurl['host'] == 'www.woquge.com') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $title = cut('<h1>', '</h1>', $result);
            $body = cut('<div id="content">', '</div>', $result);
            if(empty($body)) {
                $body = cut('<div id="content">', '</div>', $result);
            }
        }
        elseif ($parseurl['host'] == 'www.biquge.com.tw') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $title = cut('<h1>', '</h1>', $result);
            $body = cut('<div id="content">', '</div>', $result);
        }
        elseif ($parseurl['host'] == 'www.vodtw.com') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $title = cut('<h1>', '</h1>', $result);
            $title = strip_tags($title);
            preg_match('/<div id="trail"[^>]*><\/div>(.*?)<\/div>/is', $result, $match);
            $body = trim($match[1]);
            $body = preg_replace('/<p>本书来自(.*?)\.html/i', '', $match[1]);

            if(!$body && $snoopy && $snoopy->status == 200) {
                $cookiedata = cut('var data =', ';', $result);
                $cookiedata = preg_replace('/\s+/', '', $cookiedata);
                preg_match_all('/\'(.*?)\':"(.*?)"/i', $cookiedata, $matches);
                if($matches[2]) {
                    $snoopy->cookies[$matches[2][0]] = $matches[2][1];
                    $snoopy->cookies[$matches[2][2]] = $matches[2][3];
                }

                //echo $result;
            }
        }
        else {
            print_r($parseurl);
            return false;
        }

        return [$title, $body];
    }

    function getListBook($url, $html, $snoopy = null) {
        $parseurl = parse_url($url);
        if ($parseurl['host'] == 'www.biquge.com.tw') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            $rlist = cut('<div class="r">', '</div>', $result);
            $llist = cut('<div class="l">', '</div>', $result);

            preg_match_all('/<li>(.*?)<\/li>/i', $llist, $matches1);
            preg_match_all('/<li>(.*?)<\/li>/i', $rlist, $matches2);

            foreach ($matches2[1] as $item) {
                preg_match('/<a href="(.*?)"[^>]*>(.*?)<\/a>/i', $item, $matches4);
                preg_match('/<span class="s5">(.*?)<\/span>/i', $item, $match);

                $url = $matches4[1];
                $title = $matches4[2];
                if(!strstr($url, 'http')) {
                    $url = 'http://'.$parseurl['host'].$url;
                }

                $index_data = array(
                    'md5id' => md5($url),
                    'url' => $url,
                    'title' => $title,
                    'last_chapter_url' => '',
                    'last_chapter' => '',
                    'author' => $match[1],
                    'size' => mt_rand(1,99).' kb',
                    'updatetime' => time(),
                    'state' => '连载中',
                    'indextime' => time(), //索引添加时间
                );

                $data[] = $index_data;  
            }

            foreach ($matches1[1] as $item) {
                preg_match_all('/<a href="(.*?)"[^>]*>(.*?)<\/a>/i', $item, $matches3);
                preg_match('/<span class="s5">(.*?)<\/span>/i', $item, $match);

                $url = $matches3[1][0];
                $title = $matches3[2][0];
                $last_chapter_url = $matches3[1][1];
                $last_chapter = $matches3[2][1];
                if(!strstr($url, 'http')) {
                    $url = 'http://'.$parseurl['host'].$url;
                }
                if(!strstr($last_chapter_url, 'http')) {
                    $last_chapter_url = 'http://'.$parseurl['host'].$last_chapter_url;
                }

                $index_data = array(
                    'md5id' => md5($url),
                    'url' => $url,
                    'title' => $title,
                    'last_chapter_url' => $last_chapter_url,
                    'last_chapter' => $last_chapter,
                    'author' => $match[1],
                    'size' => mt_rand(1,99).' kb',
                    'updatetime' => time(),
                    'state' => '连载中',
                    'indextime' => time(), //索引添加时间
                );

                $data[] = $index_data;  
            }
        }
        elseif ($parseurl['host'] == 'www.vodtw.com') {
            $result = iconv('GBK', 'utf-8//IGNORE', $html);

            preg_match_all('/<div class="yl_nr_lt2">(.*?)<\/div>/is', $result, $matches);
            foreach ($matches[1] as $item) {
                preg_match_all('/href="(.*?)"[^>]*>(.*?)<\/a>/i', $item, $matches2);
                preg_match('/<li class="ynl5">(.*?)<\/li>/i', $item, $match1);
                preg_match('/<li class="ynl7">(.*?)<\/li>/i', $item, $match2);

                $url = $matches2[1][1];
                $title = $matches2[2][1];
                $last_chapter_url = $matches2[1][2];
                $last_chapter = $matches2[2][2];
                $author = $matches2[2][3];
                $state = $match1[1];
                if(empty($last_chapter_url)) {
                    echo2($title.', '.$url.', last_chapter_url empty');
                    continue;
                }

                if(!strstr($last_chapter_url, 'http')) {
                    $last_chapter_url = 'http://'.$parseurl['host'].$last_chapter_url;
                }
                $lcu = explode('/', $last_chapter_url);
                array_pop($lcu);
                $lcu[] = 'Index.html';
                $url = implode('/', $lcu);

                $index_data = array(
                    'md5id' => md5($url),
                    'url' => $url,
                    'title' => $title,
                    'last_chapter_url' => $last_chapter_url,
                    'last_chapter' => $last_chapter,
                    'author' => $author,
                    'size' => mt_rand(1,99).' kb',
                    'updatetime' => time(),
                    'state' => $state,
                    'indextime' => time(), //索引添加时间
                );

                $data[] = $index_data;  
            }

            if(!$data && $snoopy && $snoopy->status == 200) {
                $cookiedata = cut('var data =', ';', $result);
                $cookiedata = preg_replace('/\s+/', '', $cookiedata);
                preg_match_all('/\'(.*?)\':"(.*?)"/i', $cookiedata, $matches);
                if($matches[2]) {
                    $snoopy->cookies[$matches[2][0]] = $matches[2][1];
                }
            }
        }
        else {
            print_r($parseurl);
            return false;
        }
        
        return $data;
    }
}